Are there any pitfalls to var something = something || {}

Posted by puk on Stack Overflow See other posts from Stack Overflow or by puk
Published on 2011-11-17T21:30:36Z Indexed on 2011/11/20 17:51 UTC
Read the original article Hit count: 344

Filed under:
|

I came across this answer where the poster suggested that the shorthand for

if(typeof MyNamespace === 'undefined'){
    var MyNamespace = {};
}

is

var MyNamespace = MyNamespace || {};

Would veteran programmers recommend the latter to simplify the code, or does it overly complicate things, like abusing ++ or -- in complex compound statements?


EDIT The reason I asked is b/c a while back someone inspired me by pointing out that a lot of the people who think they are expert programmers make a lot of beginners mistakes. The case at the time was

if (isReady){
  //Do Something
}

And what he was saying is that a condition should mean something, isReady doesn't 'mean' anything, instead, we should use

if (isReady === true){
  //Do Something
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about syntax